package com.zrwl.poscloud.xo.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.User;
import com.zrwl.poscloud.base.entity.UserEmployeeR;
import com.zrwl.poscloud.base.superI.SuperMapper;
import com.zrwl.poscloud.xo.vo.UserEmployeeRVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * 用户关注持卡人员 Mapper类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-02-24
 */
public interface UserEmployeeRMapper extends SuperMapper<UserEmployeeR> {

    @Select("<script>" +
            " select" +
            "  r.uid,r.status,r.employee_uid,r.create_time createTime,r.type,r.update_time updateTime," +
            "  u.nick_name nickName,u.user_name userName,u.mobile,e.name employeeName," +
            "  e.cert_code certCode,e.employee_code employeeCode,e.company_code companyCode," +
            "  d.name deptName,c.short_name companyName" +
            " from t_user_employee_r r left join t_user u on r.user_uid=u.uid " +
            " left join t_employee e on r.employee_uid=e.uid " +
            " left join t_dept d on e.company_code=d.company_code " +
            " left join t_company c on c.company_code=substr(d.company_code,1,5)" +
            "<where>" +
            "   <if test='param.userUid!=null and param.userUid!=&quot;&quot;'> " +
            "   and r.user_uid = #{param.userUid}" +
            "   </if>" +
            "   <if test='param.keyword!=null and param.keyword!=&quot;&quot;'> " +
            "   and (u.nick_name LIKE CONCAT('%',#{param.keyword},'%') or e.name LIKE CONCAT('%',#{param.keyword},'%') or e.cert_code = #{param.keyword}  or e.employee_code = #{param.keyword} or u.mobile = #{param.keyword} or u.user_name LIKE CONCAT('%',#{param.keyword},'%'))" +
            "   </if>" +
            "   <if test='param.employeeUid!=null and param.employeeUid!=&quot;&quot;'>" +
            "    and r.employee_uid = #{param.employeeUid}" +
            "   </if>" +
            "   <if test='param.status == 0'> " +
            "       and r.status != 0 " +
            "   </if> " +
            "   <if test='param.status == -1'> " +
            "       and r.status = 0 " +
            "   </if> " +
            "   <if test='param.status != -1 and param.status != 0'> " +
            "       and r.status = #{param.status} " +
            "   </if> " +
            "</where>" +
            "</script>"
    )
    IPage<UserEmployeeRVO> allList(Page page, @Param("param") UserEmployeeRVO userEmployeeRVO);//admin用未修改无方案

    @Select("<script>" +
            " select r.uid,r.status,r.employee_uid,r.create_time createTime,r.type,r.update_time updateTime, " +
            "       u.nick_name nickName,u.user_name userName,u.mobile,e.name employeeName, " +
            "       e.cert_code certCode,e.employee_code employeeCode,e.company_code companyCode, " +
            "       c.company_name " +
            "from t_user_employee_r r left join t_user u on r.user_uid=u.uid " +
            "                         left join t_employee e on r.employee_uid=e.uid " +
            "                                     LEFT JOIN (select uid company_uid, short_name company_name, company_code " +
            "                                                from t_company " +
            "                                                where status != 0 " +
            "                                                union " +
            "                                                select uid company_uid, name company_name, company_code " +
            "                                                from t_dept " +
            "                                                where status != 0) c ON c.company_code=e.company_code " +
            "        where e.employee_code = #{param.keyword} and r.company_uid = #{param.companyUid}" +
            "      <if test='param.type != 0'> " +
            "            and r.type = #{param.type} " +
            "       </if> " +
            "      <if test='param.deptRoleTree != null and param.deptRoleTree.length>0'> " +
            "            and c.company_code in " +
            "           <foreach item='item' index='index' collection='param.deptRoleTree' open='(' separator=',' " +
            "                   close=')'> " +
            "               #{item} " +
            "           </foreach> " +
            "       </if> " +
            "</script>"
    )
    List<UserEmployeeRVO> getUserEmployeeRByEmployeeCode(@Param("param") UserEmployeeRVO userEmployeeRVO);//admin用未修改无方案

//    @Select("<script>" +
//            " select" +
//            " r.uid,r.status,r.type," +
//            " u.uid userUid,u.nick_name nickName,u.user_name userName,u.mobile,e.uid employeeUid,e.name employeeName, " +
//            " e.cert_code certCode,e.employee_code employeeCode,e.company_code companyCode, " +
//            " d.uid deptUid,d.name deptName,c.uid companyUid,c.full_name companyFullName,c.short_name companyName " +
//            " from t_user_employee_r r left join t_user u on r.user_uid=u.uid " +
//            " left join t_employee e on r.employee_uid=e.uid " +
//            " left join t_dept d on e.company_code=d.company_code " +
//            " left join t_company c on c.company_code=substr(d.company_code,1,5) " +
//            " where r.user_uid = #{param.userUid} and r.employee_uid = #{param.employeeUid}" +
//            " and (r.status =1 or r.status=3) " +
//            "</script>"
//    )
////leftjoin的位置要注意所有的status判断
//    UserEmployeeRVO selectFollowDetail(@Param("param") UserEmployeeRVO userEmployeeRVO);

    @Select("<script>" +
            " select" +
            " r.uid,r.status,r.type,r.employee_json_data employeeJsonData," +
            " u.uid userUid,u.nick_name nickName,u.user_name userName,u.mobile " +
            " from t_user_employee_r r left join t_user u on r.user_uid=u.uid " +
            " where r.user_uid = #{param.userUid} and r.employee_uid = #{param.employeeUid}" +
            " and (r.status =1 or r.status=3) " +
            "</script>"
    )
//leftjoin的位置要注意所有的status判断
    UserEmployeeRVO selectFollowDetail(@Param("param") UserEmployeeRVO userEmployeeRVO);

//    @Select("<script>" +
//            " select" +
//            " r.uid,r.status,r.type," +
//            " u.uid userUid,u.nick_name nickName,u.user_name userName,u.mobile,e.uid employeeUid,e.name employeeName, " +
//            " e.cert_code certCode,e.employee_code employeeCode,e.company_code companyCode, " +
//            " d.uid deptUid,d.name deptName,c.uid companyUid,c.full_name companyFullName,c.short_name companyName " +
//            " from t_user_employee_r r left join t_user u on r.user_uid=u.uid " +
//            " left join t_employee e on r.employee_uid=e.uid " +
//            " left join t_dept d on e.company_code=d.company_code " +
//            " left join t_company c on c.company_code=substr(d.company_code,1,5) " +
//            " where r.user_uid = #{param.userUid} and (r.status =1 or r.status=3) " +
//            "</script>"
//    )
//    List<UserEmployeeRVO> selectAllPresentUserFollowList( @Param("param") UserEmployeeRVO userEmployeeRVO);

    @Select("<script>" +
            " select" +
            " r.uid,r.status,r.type,r.employee_json_data employeeJsonData," +
            " u.uid userUid,u.nick_name nickName,u.user_name userName,u.mobile,c.full_name companyFullName " +
            " from t_user_employee_r r left join t_user u on r.user_uid=u.uid " +
            " left join t_company c on r.company_uid=c.uid " +
            " where r.user_uid = #{param.userUid} and (r.status =1 or r.status=3) " +
            "</script>"
    )
    List<UserEmployeeRVO> selectAllPresentUserFollowList(@Param("param") UserEmployeeRVO userEmployeeRVO);

    int unlink(@Param("employeeUid") String employeeUid);

    UserEmployeeRVO getCurrentFollowersByEmployeeUid(@Param("employeeUid") String employeeUid);


    @Update("<script>" +
            " UPDATE t_user_employee_r t" +
            " set t.update_time= #{updateTime},t.status=#{status} WHERE t.employee_uid=#{employeeUid} " +
            "</script>"
    )
    int updateStatusByEmployeeUid(@Param("employeeUid") String employeeUid, @Param("status") int status, @Param("updateTime") Date date);
    List<UserEmployeeRVO> getByEmployeeUid(@Param("employeeUid") String employeeUid, @Param("type") int type);

    @Select("<script>" +
            "select * from t_user u where u.uid " +
            "in (select r.user_uid from t_user_employee_r r " +
            "where r.employee_uid=#{employeeUid} and r.status !=0) " +
            "and u.status !=0</script>"
    )
    List<User> getUserByEmployeeUid(@Param("employeeUid")String toUid);
}
